package net.tycmc.cty.ctyms.timejob.iems.dao;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import com.tycmc.functions.util.NumberFormat;

import net.tycmc.bulb.common.dao.BaseDAO;
import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.dao.TransactionDAO;
import net.tycmc.bulb.common.datasource.ProxoolDataSource;
import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.DateRegUtil;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.common.MyBaseDAO;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSDAO;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSTransDAO;

public class DrivingBehaviorScoreDAO {
	private IBaseDAO baseDAO;
	
	private ITransactionDAO tranDAO;
	
	
	

	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	/**
	 * @return the baseDAO
	 */
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	/**
	 * @param baseDAO the baseDAO to set
	 */
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	/**获取类比发动机的规则数据
	 * @param param
	 * @return
	 */
	public List<Map<String,Object>> getTheEngRule(){
		String sql= "select DBSR_EgnType,"+
					"DBSR_BeKind,"+
					"DBSR_BeEvent,"+
					"DBSR_BaScore,"+
					"DBSR_StaValue,"+
					"DBSR_ChaValue,"+
					"DBSR_ChaScore "+
					"from DriverBehaviorScoreRuel inner join BehaviorEvent_Set on DBSR_BeEvent=BES_ID order by DBSR_EgnType,DBSR_BeEvent";
		List<Map<String,Object>>list=this.baseDAO.execQuery(sql,new Object[]{});
		return list;
	}
	/**
	 *驾驶行为
	 * @author jiyongtian 20140530 IEMS3.6 新增
	 **/
	public List<Map<String, Object>> getVclInfo(String workDate) {
		StringBuffer strBuf =new StringBuffer();
		String startMonth=workDate.substring(0, 10).replace("-", "");
		StringBuffer sbSql=new StringBuffer();
				sbSql.append("(select MsgESS_Vcl_ID,");
				sbSql.append("MsgESS_iWork as toRunTime,");
				sbSql.append("MsgESS_iOilCons as toRunOil,");
				sbSql.append("MsgESS_iRunKm,");
//				sbSql.append("MsgESS_iRunKmECM,");
//				sbSql.append("MsgESS_iRunKmGPS,");
//				sbSql.append("MsgESS_iRunKmExact,");
//				sbSql.append("MsgESS_iRunKmExact_ECM,");
//				sbSql.append("MsgESS_iRunKmECU,");
				//平均载重
				sbSql.append("MsgESS_AverageLoad as avgLoad,");
		    	//驾驶行为相关
		    	//超长怠速次数
		    	sbSql.append("MsgESS_LongIdle AS longIdleTimes,");
		    	//超长怠速时长
		    	sbSql.append(" MsgESS_LongIdleWorkTime AS longIdleTime,");
		    	//低档高速次数
		    	sbSql.append(" MsgESS_LowSpeedNum AS lowHighTimes,");
		    	//低档高速时长
		    	sbSql.append(" MsgESS_LowSpeedWorkTime AS lowHighTime,");
		    	//低档高速里程
		    	sbSql.append(" MsgESS_LowSpeedRunKm AS lowHighKm,");
		    	//急加速次数
		    	sbSql.append(" MsgESS_RapidSpeedNum AS JJSTimes,");
		    	//急加速里程
		    	sbSql.append(" MsgESS_RapidSpeedKm AS JJSKm,");
		    	//全油门次数
		    	sbSql.append(" MsgESS_AcceleratorNum AS QYMTimes,");
		    	//全油门里程
		    	sbSql.append(" MsgESS_AcceleratorKm AS QYMKm,");
		    	//急刹车次数
		    	sbSql.append(" MsgESS_SlamBrakesNum AS JSCTimes, ");
		    	//疲劳驾驶次数
		    	sbSql.append(" MsgESS_FatigueDrivingNum AS PLJSTimes, ");
		    	//疲劳驾驶时长
		    	sbSql.append(" MsgESS_FatigueDrivingWorkTime AS PLJSTime, ");
		    	//定速巡航里程
		    	sbSql.append(" MsgESS_CruiseKm AS DSXHKm, ");
		    	//空挡踩油门次数
		    	sbSql.append(" MsgESS_NeutralThrottleNum AS KDCYMTimes, ");
		    	//空挡滑行次数
		    	sbSql.append(" MsgESS_NeutralRunNum AS KDHXTimes, ");
		    	//空挡滑行时长
		    	sbSql.append(" MsgESS_NeutralRunWorkTime AS KDHXTime, ");
		    	//高速行驶次数
		    	sbSql.append(" MsgESS_OverSpeedNum_1 AS GSXSTimes, ");
		    	//高速行驶里程
		    	sbSql.append(" MsgESS_OverSpeedRunKm_1 AS GSXSKm, ");
		    	//高速行驶时长
		    	sbSql.append(" MsgESS_OverSpeedWorkTime_1 AS GSXSTime, ");
		    	//超速行驶次数
		    	sbSql.append(" MsgESS_OverSpeedNum_2 AS CSXSTimes, ");
		    	//超速行驶里程
		    	sbSql.append(" MsgESS_OverSpeedRunKm_2 AS CSXSKm, ");
		    	//超速行驶时长
		    	sbSql.append(" MsgESS_OverSpeedWorkTime_2 AS CSXSTime, ");
		    	//刹车次数
		    	sbSql.append(" MsgESS_BrakeNum AS SCTimes, ");
		    	//超转速次数
		    	sbSql.append(" MsgESS_SuperSpeedNum AS CZSTimes, ");
		    	//超转速时长
		    	sbSql.append(" MsgESS_SuperSpeedTime AS CZSTime,MsgESS_State,MsgESS_Complete ");
		    	sbSql.append("from "+GlobalDBVar.getDBFullName_Conn("Msg_EquipmentState_Statistics_3rd_"+startMonth, "3", null)+")");
		strBuf.append("select *,");
		strBuf.append("toRunKm = MsgESS_iRunKm ");
		//strBuf.append("toRunKm = case ISNULL(vv.VehicleRumKmID,1) when 1 then ISNULL(MsgESS_iRunKmExact,0) when 2 then ISNULL(MsgESS_iRunKmExact_ECM,0) when 3 then ISNULL(MsgESS_iRunKmECM,0) when 4 then ISNULL(MsgESS_iRunKmECU,0) when 5 then ISNULL(MsgESS_iRunKmGPS,0) end  ");
		strBuf.append("from v_inteloilnew vv  inner join "+sbSql.toString()+" mst on mst.MsgESS_Vcl_ID = vv.VehicleID ");
		strBuf.append("where 1=1 and EngineModel is not null and vv.UsefulLife > GETDATE() and vv.Tmnl_SoftEdition is not null and vv.Tmnl_SoftEdition <> '' ");
		strBuf.append("and MsgESS_State = 0 and MsgESS_Complete=1");
		return this.baseDAO.execQuery(strBuf.toString(), new Object[]{});
	}
	
	
	/**
	 * 从档位及车速分作业中查询除其他区以外的区占比
	 **/
	private List<Map<String, Object>> queryGearingSQLFromDBStatic(String workDate) {
		StringBuffer sql = new StringBuffer();
		StringBuffer Msql = new StringBuffer();
		String startMonth=workDate.substring(0, 7).replace("-", "");
			Msql.append("select vcldb2_vcl_id,VclDB2_Pre,"+
					"isnull(VclDB2_iWork,0)VclDB2_iWork,"+
					"VclDB2_BD_ID,vcldb2_statetime,isnull(VclDB2_totalCnt,0)totalCnts,isnull(vcldb2_0g1stimes,0)vcldb2_0g1stimes,isnull(vcldb2_0g3stimes,0)vcldb2_0g3stimes "+
					"from vcldb2_"+startMonth+" where VclDB2_totalCnt is not null and VclDB2_totalCnt!=0");
			
		sql.append("select isnull(sum(ma.VclDB2_Pre*ma.totalCnts),0)as bili,isnull(sum(ma.VclDB2_Pre*ma.VclDB2_iWork),0)as iworkbili," +
				"isnull(sum(vcldb2_0g1stimes),0)as total0G1S,isnull(sum(vcldb2_0g3stimes),0)as total0G3S,isnull(sum(ma.totalCnts),0)totalCnt,isnull(sum(ma.VclDB2_iWork),0)twork,ma.VclDB2_Vcl_ID,ma.VclDB2_BD_ID from ("+Msql.toString()+")ma");
		sql.append(" inner join v_intelOilNew temptab on ma.VclDB2_Vcl_ID = temptab.VehicleID  where 1=1 ");
		sql.append(" and VclDB2_StateTime>=? and VclDB2_StateTime<=? group by VclDB2_BD_ID,vcldb2_vcl_id");
		return this.getBaseDAO().execQuery(sql.toString(), new Object[]{DateUtil.addDay(-1),DateUtil.addDay(-1)});
	}
	public Map<String,Map<String,Object>>Db2Operate(String workDate){
		List<Map<String, Object>>listDRH=this.queryGearingSQLFromDBStatic(workDate);
		Map<String,List<Map<String,Object>>>mapResult=new ListUtil().groupBy("vcldb2_vcl_id", listDRH);
		Set<String>set=mapResult.keySet();
		Map<String,Map<String,Object>>Mapresult=new HashMap<String, Map<String,Object>>();
		for(Iterator<String>it=set.iterator();it.hasNext();){
			double totalCnt = 0;
			double tDriPer = 0;// 用于判断是否驾驶行为II有值
			String keyVclId=it.next();
			Map<String,Object>reMap=new HashMap<String, Object>();
			List<Map<String,Object>>listVcl=mapResult.get(keyVclId);
			for (Map<String, Object> map : listVcl) {
				if("1".equals(MapGetter.getString(map, "VclDB2_BD_ID"))){
					//获取工作时长
					totalCnt = StringUtil.isValid(MapGetter.getString(map, "totalCnt"))?Double.parseDouble(MapGetter.getString(map, "totalCnt")):0;
				}
				if (!"3".equals(MapGetter.getString(map, "VclDB2_BD_ID"))) {
					tDriPer += StringUtil.isValid(MapGetter.getString(map, "bili"))?Double.parseDouble(MapGetter.getString(map, "bili")):0;
				}
			}
			if (tDriPer == 0) {
				reMap.put("ecoArea", "0");
				reMap.put("loHiArea", "0");
				reMap.put("hiLoArea", "0");
			} else {
				double ecoArea=0;
				for (Map<String, Object> map : listVcl) {
					String DriType = MapGetter.getString(map, "VclDB2_BD_ID");
					double bili = StringUtil.isValid(MapGetter.getString(map, "bili"))?Double.parseDouble(MapGetter.getString(map, "bili")):0;
					
					if ("1".equals(DriType) && totalCnt>0) {//最佳经济区
						ecoArea+=NumberFormat.div(bili, totalCnt, 2);
					}else if("1".equals(DriType)){
						ecoArea+=0;
					}
					if ("2".equals(DriType) && totalCnt>0) {//一般经济区
						ecoArea+= NumberFormat.div(bili, totalCnt, 2);
					}else if("2".equals(DriType)){
						ecoArea+=0;
					}
					if ("5".equals(DriType) && totalCnt>0) {//低档高速
						reMap.put("loHiArea", NumberFormat.div(bili, totalCnt, 2));
					}else if("5".equals(DriType)){
						reMap.put("loHiArea", "0");
					}
					if ("6".equals(DriType) && totalCnt>0) {//高档低速
						reMap.put("hiLoArea", NumberFormat.div(bili, totalCnt, 2));
					}else if("6".equals(DriType)){
						reMap.put("hiLoArea", "0");
					}
     				}
				reMap.put("ecoArea", ecoArea);
			}
			Mapresult.put(keyVclId,reMap);
		}
		return Mapresult;
	}
	public boolean execBatch(List<String>sqlList){
		boolean flag=tranDAO.batch(sqlList);
		return flag;
	}
}
